#ifndef CLIENTDEFECT_H
#define CLIENTDEFECT_H

#include <QObject>
#include <QList>
#include <QQmlListProperty>
#include <QMutex>
#include<QJsonObject>
/**
 * @brief SteelInfo 存储的是钢卷信息
 * @brief ClientDefect 存储数据库访问的缺陷数据
 */
class ClientDefect : public QObject
{
    Q_OBJECT
public:
    explicit ClientDefect(QObject *parent = nullptr);

    ClientDefect(int cno, int dno, int w, int h, int a, int clz, QString dname, int l, int r,int b, int t,int imgidx, int lv )
        :CameraNo(cno),defectNo(dno),width(w),height(h),Area(a),DefectClass(clz),DefectName(dname),
        LeftinSteel(l),RightinSteel(r),BottomInSteel(b),TopinSteel(t),ImageIndex(imgidx),defectLevel(lv)
        {};


    int getCameraNo()const{return this->CameraNo;}
    void setCameraNo(const int &No){ this->CameraNo = No; };

    int getDefectNo()const{return this->defectNo;}
    void setDefectNo(const int &No){ this->defectNo = No; }

    int getWidth()const{return this->width;}
    void setWidth(const int &w){ this->width = w;   }

    int getHeight()const{return this->height;}
    void setHeight(const int &h){ this->height = h; }

    int getArea()const{return this->Area;}
    void setArea(const int &a){ this->Area = a; }

    int getDefectClass() const{return this->DefectClass;}
    void setDefectClass(const int &d){ this->DefectClass = d;  }

    const QString &getDefectName()const{ return this->DefectName;  };
    void  setDefectName(const QString &name){ this->DefectName = name; };

    int getLeftinSteel()const{return this->LeftinSteel;}
    void setLeftinSteel(const int &left){ this->LeftinSteel = left; }

    int getRightintSteel()const {return this->RightinSteel;}
    void setRightinSteel(const int &right){ this->RightinSteel = right; }

    int getBottomInSteel() const {return this->BottomInSteel;}
    void setBottomInSteel(const int &bottom){ this->BottomInSteel = bottom; }

    int getTopinSteel() const{return this->TopinSteel;}
    void setTopinSteel(const int &top){ this->TopinSteel = top; };

    int getImageIndex() const{ return this->ImageIndex; }
    void SetImageIndex(int imgindex){ this->ImageIndex = imgindex; };

    int getDefectLevel() const { return this->defectLevel; }
    void SetDefectLevel(const int & lv) { this->defectLevel = lv; }


public:
    QJsonObject toJson(){
        QJsonObject json;
        json["cameraNo"] = CameraNo;
        json["defectNo"] = defectNo;
        json["width"] = width;
        json["height"] = height;
        json["area"] = Area;
        json["defectClass"] = DefectClass;
        json["defectName"] = DefectName;
        json["leftinSteel"] = LeftinSteel;
        json["rightinSteel"] = RightinSteel;
        json["bottomInSteel"] = BottomInSteel;
        json["topinSteel"] = TopinSteel;
        json["imageIndex"] = ImageIndex;
        json["defectLevel"] = defectLevel;
        return json;
    }

private:
    int CameraNo;
    int defectNo;
    int width;
    int height;
    int Area;
    int DefectClass;
    QString DefectName;
    int LeftinSteel;
    int RightinSteel;
    int BottomInSteel;
    int TopinSteel;
    int ImageIndex;
    int defectLevel;

};



/**
 * @brief 该类是用于数据库的钢卷信息类
 * 1.通过该类你可以访问到所有的数据
 */
class SteelInfo : public QObject{
    Q_OBJECT;

public:
    QString getSteelName()const{
        return SteelName;
    }
    void setSteelName(const QString &sName){
        QMutexLocker locker(&mutex);
        SteelName = sName;
    }

    QString getSteelType() const  {
        return SteelType;
    }

    void setSteelType(const QString &newSteelType)
    {
        QMutexLocker locker(&mutex);
        SteelType = newSteelType;
    }



    int getLength()const{ return this->length; }
    void setLength(const int &l){
        QMutexLocker locker(&mutex);
        this->length = l;
    }

    int getWidth()const{ return this->width; }
    void setWidth(const int &w){
        QMutexLocker locker(&mutex);
        this->width = w;
    };

    int getThick()const{ return this->thick; }
    void setThick(const int &t){
        QMutexLocker locker(&mutex);
        this->thick = t;
    }

    int getTopLen()const{ return this->TopLen; }
    void setTopLen(const int &toplen){
        QMutexLocker locker(&mutex);
        this->TopLen = toplen;
    }

    int getSteelNo()const{ return this->steelNo; };
    void setSteelNo(const int &no){
        QMutexLocker locker(&mutex);
        this->steelNo = no;
    }

    int getLevel()const{ return this->Level; }
    void setLevel(const int &lv){
        QMutexLocker locker(&mutex);
        this->Level = lv;
    }

public:

    //根据缺陷名称 查找大于该等级的缺陷总数
    int getTotalDefectNumber(QString defectName,int Level){
        int totalCount = 0;
        foreach (ClientDefect *defect, ClientDefect1ist) {
            if( (defect->getDefectName().compare(defectName)==0) && defect->getDefectLevel() == Level){
                totalCount++;
            }
        }
        return totalCount;
    };

signals:
    void ClentDefectChanged();


public:
    QMutex mutex;

    QStringList infos;//判级描述
    QList<ClientDefect*> ClientDefect1ist;//将所有相机的数据整合到了一起

    QSet<QString> DefectNameList;//该卷出现的所有缺陷

    void AddToThreeLevelMap(QString DefectName){
        if(ThreeLvDefectMap.contains(DefectName)){
            ThreeLvDefectMap[DefectName] += 1;
        }
        else
        {
            ThreeLvDefectMap.insert(DefectName,1);
        }
    }

    void AddToTwolevelMap(QString DefectName){
        if(TwoLvDefectMap.contains(DefectName)){
            TwoLvDefectMap[DefectName] += 1;
        }
        else
        {
            TwoLvDefectMap.insert(DefectName,1);
        }
    }

    void AddToOneLevelMap(QString DefectName){
        if(OneLvDefectMap.contains(DefectName)){
            OneLvDefectMap[DefectName] += 1;
        }
        else
        {
            OneLvDefectMap.insert(DefectName,1);
        }
    }

    //记录存在等级的缺陷 以及数量
    QMap<QString,int> OneLvDefectMap;
    QMap<QString,int> TwoLvDefectMap;
    QMap<QString,int> ThreeLvDefectMap;

private:
    QString SteelName;
    QString SteelType;
    int length;
    int width;
    int thick;
    int TopLen;
    int steelNo;
    int Level; //当前钢卷等级


};

#endif // CLIENTDEFECT_H
